package com.lmabbe.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.tenant.objects.converter.TenantInfoConverter;
import com.lmabbe.tenant.objects.dto.TenantInfoDTO;
import com.lmabbe.tenant.objects.dto.TenantRenewalDTO;
import com.lmabbe.tenant.objects.entity.TenantInfo;
import com.lmabbe.tenant.objects.query.TenantInfoQuery;
import com.lmabbe.tenant.objects.vo.TenantInfoVO;
import com.lmabbe.tenant.service.OauthClientService;
import com.lmabbe.tenant.service.TenantDatabasesService;
import com.lmabbe.tenant.service.TenantInfoService;
import com.lmabbe.tenant.service.TenantMiniappService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户信息表管理
 *
 * @author lmabbe
 */
@Api(tags = "租户信息表管理")
@RestController
@RequestMapping("tenant")
@RequiredArgsConstructor
public class TenantInfoController {

    private final TenantInfoService tenantInfoService;
    private final TenantInfoConverter tenantInfoConverter;
    private final TenantMiniappService tenantMiniappService;
    private final TenantDatabasesService tenantDatabasesService;
    private final OauthClientService oauthClientService;

    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<TenantInfoVO>> list() {
        List<TenantInfo> list = tenantInfoService.list();
        return R.success(tenantInfoConverter.list2Vos(list));
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<TenantInfoVO>> list(TenantInfoQuery query) {
        IPage page = tenantInfoService.page(query.createPage(), query.createQuery());
        page.setRecords(tenantInfoConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<TenantInfoVO> getById(@PathVariable Long id) {
        return R.success(tenantInfoConverter.entity2Vo(tenantInfoService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody TenantInfoDTO dto) {
        this.tenantInfoService.save(tenantInfoConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{tenantId}")
    public R update(@RequestBody TenantInfoDTO dto, @PathVariable Long tenantId) {
        this.tenantInfoService.updateById(tenantInfoConverter.dto2Entity(dto, tenantId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.tenantInfoService.removeById(id);
        return R.success();
    }


    @Operation(summary = "获取租户所有小程序")
    @GetMapping("/{tenantId}/miniapps")
    public R miniapps(@PathVariable String tenantId) {
        return R.success(tenantMiniappService.findTenantAllMiniApp(tenantId));
    }

    @Operation(summary = "获取租户所有数据库")
    @GetMapping("/{tenantId}/databases")
    public R databases(@PathVariable String tenantId) {
        return R.success(tenantDatabasesService.findTenantAllDatabases(tenantId));
    }

    @Operation(summary = "获取租户所有授权客户端")
    @GetMapping("/{tenantId}/oauthClient")
    public R oauthClient(@PathVariable String tenantId) {
        return R.success(oauthClientService.findTenantAllOauthClient(tenantId));
    }


    @Operation(summary = "续租")
    @PostMapping("{tenantId}/renewal")
    public R renewal(@PathVariable Long tenantId, @RequestBody TenantRenewalDTO tenantRenewalDTO) {
        tenantInfoService.renewal(tenantId, tenantRenewalDTO);
        return R.success();
    }

}